package com.damionew.website.service.back;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.catalina.mbeans.RoleMBean;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.damionew.website.dao.back.MemberDao;
import com.damionew.website.dao.back.RoleDao;
import com.damionew.website.model.back.Member;
import com.damionew.website.model.back.Member_Role;
import com.damionew.website.model.back.Permission;
import com.damionew.website.model.back.Role;
import com.damionew.website.model.back.Role_Permission;

@Service
public class MemberService {
	@Autowired
	MemberDao memberDao;
	@Autowired
	RoleDao roleDao;
	//获取角色
	public Set<String> findRoles(String account){
		Set<String> roles = new HashSet<String>();
		Member member = memberDao.findMemberByAccount(account);
		List<Member_Role> member_Roles = member.getMember_roles();
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		session.setAttribute("mr", "112");
		for(Member_Role mr:member_Roles) {
			Role role = mr.getRole();
			roles.add(role.getRole());
		}
		System.out.println("获取角色");
		return roles;
	}
	//获取权限
	public Set<String> findPermissions(String account){
		Set<String> permissions = new HashSet<String>();
		Member member = memberDao.findMemberByAccount(account);
		List<Member_Role> member_Roles = member.getMember_roles();
		for(Member_Role mr:member_Roles) {
			Role role = roleDao.findRoleById(mr.getRole().getRole_id());
			List<Role_Permission> role_Permissions = role.getRole_permissions();
			for(Role_Permission rp:role_Permissions) {
				String permission = rp.getPermission().getPermission();
				permissions.add(permission);
			}
		}
		System.out.println("获取权限");
		return permissions;
	}
	public Member getMemberLogin(String account) {
		Member member = memberDao.getMemberLogin(account);
		return member;
	}

	public List<Member> getMemberList(String sortName, String sortOrder) {
		List<Member> list = memberDao.getMemberList(sortName, sortOrder);
		return list;
	}

	public void addMember(Member member) {
		memberDao.addMember(member);
	}

	public void updateMember(Member member) {

		memberDao.updateMember(member);
	}

	public void deleteMember(String id) {
		memberDao.deleteMember(id);
	}

	public int getMemberTotal() {
		List<Member> list = memberDao.getMemberTotal();
		int total = list.size();
		return total;
	}
}
